home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr10 / v12n12.zip / PCMCVT.ZIP / PCMASM.ZIP / WORDWRAP.INC < prev    next >
Text File  |  1993-04-22  |  7KB  |  210 lines

  1. ;WORDWRAP.INC
  2. ;Copyright (c) 1993 Jay Munro
  3. ;First Published in PC Magazine June 29 1993
  4. ;Word wrap function for PCMCVT.COM
  5. ;
  6. ;Variables used by for Word wrap
  7.    LastSpc    DW  0
  8.    LastLen    DW  0
  9.    KeepCX     DW  0
  10.    WriteCount DW  0
  11.    CurLine    DW  0
  12.  
  13. WordWrap Proc Near Uses BX DI SI CX, WBuffer:Word, WBLen:Word
  14. ;-----------------------------------------------------------------------
  15. ;get address of buffer, and length 
  16.    Mov   DI, WBuffer                    ;get addr of write buffer
  17.    Mov   SI, DI
  18.    Mov   BX, WBLen                      ;save length of buffer in BX
  19.    Mov   CX, BX                         ;use CX as a counter
  20.    Mov   DX, MaxWidth                   ;value for width
  21.    Or    DX,DX                          ;is it 0?
  22.    Jnz   @F
  23.    Mov   DX,78                          ;force 78 characters
  24.    Mov   MaxWidth,DX                    ;save it too
  25. @@:
  26.    Cld                                  ;forward direction
  27.    Cmp   DX,LastLen                     ;check last len for bogus size
  28.    Jg    CRLFLoop1
  29.    Mov   Word Ptr LastLen, 0            ;clear it
  30. ;-----------------------------------------------------------------------
  31.  
  32. CRLFLoop1:                              ;checks first for CRLF's
  33.    Mov   KeepCX,CX                      ;store CX for a moment
  34.    Mov   AL, 13                         ;check for that CRLF
  35.    RepNe ScaSb                          ;scan for that CRLF
  36.    Mov   AX,BX                          ;get len
  37.    Sub   AX,CX                          ;ax = length now
  38.    Mov   CurLine,AX                     ;save size
  39.    Cmp   Byte Ptr [DI],10               ;is the next character a lf?
  40.    Jnz   @F                             ;nope, skip it
  41.    Inc   CurLine                        ;bump count
  42.  
  43. @@:
  44.    Add   AX,LastLen                     ;if last length was anything...
  45.    Cmp   AX,DX                          ;check its length
  46.    Jg    PreScanLoop
  47.  
  48.    Call  WriteCurLine
  49.    Mov   AX,CurLine                     ;get last position
  50.    Add   SI,AX                          ;add it to our address
  51.    Mov   DI,SI                          ;set DI for Scan
  52.    Sub   BX,AX                          ;adjust BX (length of buffer)
  53.    Jcxz  WWDoneLp2                      ;if we're out of bytes
  54.    Mov   Word Ptr LastLen,0             ;clear last len
  55.    Jmp   CRLFLoop1
  56.  
  57. PreScanLoop:
  58.    Mov   DX, MaxWidth                   ;retread DX
  59.    Mov   CX,KeepCX
  60.    Mov   DI,SI                          ;reset DI
  61.  
  62. ;-----------------------------------------------------------------------
  63. ScanLoop:
  64.    Mov   AL, 32                         ;load AL with space character
  65.    RepNe ScaSb                          ;look for that first space
  66.    Mov   AX, BX                         ;get length
  67.    Sub   AX, CX                         ;see difference
  68.    Mov   CurLine,AX                     ;save current length
  69.    Add   AX, LastLen                    ;add in the last length
  70.    Cmp   AX, DX                         ;check line length against width
  71.    Jg    Decision2Make                  ;if it's greater, exit
  72.    Mov   AX,CurLine                     ;otherwise get current position
  73.    Mov   LastSpc,AX                     ;and save it as last space
  74.    Jmp   ScanLoop                       ;go back for more
  75.  
  76. WWDoneLeap:
  77.    Call   WriteCurLine                  ;write it
  78.  
  79. WWDoneLp2:
  80.    Mov   AX,CurLine                     ;get last length
  81.    Mov   LastLen,AX
  82.    Jmp   WWDone
  83.  
  84. Decision2Make:                          ;decide where to break
  85.    ;at this point, DI points to the next character, AX = number of chars
  86.    ;since last write crlf and BX contains starting point
  87.  
  88.    Cmp   Word Ptr LastSpc,0             ;ok, do we have at least some characters?
  89.    Jz    @F
  90.    Jmp   WritePartial
  91. @@:
  92.    Cmp   Word Ptr CurLine,0             ;how about Curline
  93.    Jz    @F
  94.    Mov   AX,CurLine                     ;set last space
  95.    Mov   LastSpc,AX
  96.    Jmp   WritePartial
  97.    
  98. @@:
  99.    Cmp   Word Ptr LastLen,0             ;was last length something?
  100.    Jz    @F
  101.    Sub   DX,LastLen                     ;adjust what we write
  102. @@:
  103.    Mov   CurLine,DX                     ;adjust current line
  104.    Call  WriteCurLine
  105.    PushF                                ;preserve flags through sub
  106.    Sub   BX,DX
  107.    PopF
  108.    Mov   CX,BX                          ;retread counter
  109.    Jnc   BackForMore                    ;check carry and write CRLF
  110.    Jmp   WWExit                         ;error exit
  111.  
  112. SetCRLF    DB  0                        ;send crlf flag
  113.  
  114. WritePartial:
  115.    Cmp   Word Ptr LastLen,0
  116.    Jz    @F
  117.    Mov   Word Ptr LastLen,0             ;reset LastLen
  118. @@:
  119.    Push  BX
  120.    Push  DX
  121.    Push  CX
  122.    Push  SI                             ;save pointer
  123.    Mov   CX, LastSpc                    ;put size into CX
  124. PartialCut:
  125.    Cmp   CX, MaxWidth
  126.    Jle   DoPartial
  127.    Call  CutLoop
  128.    Jmp   PartialCut
  129.  
  130. DoPartial:
  131.    Call  CheckNextChar                  ;check for leading space
  132.    Invoke Write, DHandle, SI, CX
  133.    Pop   SI
  134.    Pop   CX
  135.    Pop   DX
  136.    Pop   BX
  137.    Sub   BX,LastSpc
  138.    Mov   CX,BX
  139.  
  140. BackForMore:                            ;Set crlf into file
  141.    Push  BX
  142.    Push  DX
  143.    Push  CX
  144.    Invoke Write, DHandle, Addr CRLF,2   ;print a crlf
  145.    Pop   CX
  146.    Pop   DX
  147.    Pop   BX
  148.    Mov   byte Ptr SetCRLF,-1
  149.    Add   SI,LastSpc                     ;bring us up to the current location
  150.    Mov   DI,SI                          ;
  151.    Mov   AX,LastSpc                     ;
  152.    Mov   Word Ptr LastSpc, 0
  153.    Mov   Word Ptr LastLen,AX            ;preserve last length of line
  154.    Jcxz  WWDone
  155.    Mov   Word Ptr LastLen,0
  156.    Jmp   CRLFLoop1 
  157.  
  158. WWDone:
  159.  
  160. WWExit:
  161.    Ret
  162.  
  163. WriteCurLine:
  164.    Push  BX                             ;preserve our registers too
  165.    Push  CX                             ; ditto
  166.    Push  DX
  167.    Push  SI
  168.    Mov   CX, CurLine                    ;put size into CX 
  169.  
  170. CheckMWidth:
  171.    Cmp   CX, MaxWidth                   ;if current spot is greater than max width
  172.    Jle   FineLine                       ;then cut it up
  173.    Call  CutLoop
  174.    Jmp   CheckMWidth                    ;go back and check again
  175.    
  176. FineLine:
  177.    Call  CheckNextChar
  178.    Invoke Write, DHandle, SI, CX        ;write it
  179.  
  180. PopOut:
  181.    Pop   SI
  182.    Pop   DX                             ;retrieve regs
  183.    Pop   CX
  184.    Pop   BX 
  185.    RetN
  186.  
  187. CheckNextChar:                          ;trims leading spaces from word wrapped
  188.                                         ;lines 
  189.    Cmp   Byte Ptr SetCRLF,-1            ;ok, did we send a crlf last time?
  190.    Jnz   @F                             ;nope, just print it.
  191. CheckNSpace:                            ;this clips leading spaces after CRLF
  192.    Cmp   Byte Ptr [SI], 32              ;is the next character a space?
  193.    Jnz   @F                             ;no, skip
  194.    Inc   SI                             ;ok point to the next one
  195.    Loop  CheckNSpace                    ;bump back
  196. @@:
  197.    Mov   Byte Ptr SetCRLF,0             ;ok, did we send a crlf last time?
  198.    RetN
  199.  
  200. CutLoop:
  201.    Sub   CX,MaxWidth                    ;deduct maxwidth
  202.    Push  CX                             ;save CX
  203.    Invoke Write, DHandle, SI, MaxWidth  ;write only maxwidth # of char's
  204.    Invoke Write, DHandle, Addr CRLF,2   ;print a crlf
  205.    Pop   CX                             ;retrieve CX
  206.    Add   SI,MaxWidth                    ;adjust pointer to buffer
  207.    RetN
  208.  
  209. WordWrap EndP
  210.